import { formatCount, convertCurrency } from '@/utils/common.js';
import { baseUrl } from "@/utils/config.js"

function doorsViews (info) {
  var str = convertCurrency(info.receivableAmount)
  var moneyArray = []
  var index = 0
  var i = str.indexOf('整') != -1 ? 2 : 1
  for (var item of str) {
    moneyArray.push({
      type: 'text',
      text: item,
      css: {
        fontWeight: (index + i >= str.length || index % 2) ? '400' : 'bold',
        paddingLeft: '6rpx'
      }
    })
    index++
  }
  var detailViews = []
  var keys = [{ key: '', width: '44rpx', css: { borderRightWidth: '1rpx' } },
  { key: 'name', width: '150rpx', css: { borderRightWidth: '1rpx' } },
  { key: 'model', width: '157rpx', css: { borderRightWidth: '1rpx' } },
  { key: 'color', width: '80rpx', css: { borderRightWidth: '1rpx' } },
  { key: 'unitPrice', width: '80rpx', css: { borderRightWidth: '1rpx' } },
  { key: 'number', width: '80rpx', css: { borderRightWidth: '1rpx' } },
  { key: 'total', width: '80rpx' }]
  index = 0
  for (var item of info.expenseDetails) {
    index++
    for (var data of keys) {
      var view = {
        type: 'view',
        css: {
          width: data.width,
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          height: '34rpx',
          border: '0 solid #333333',
          borderBottomWidth: '1rpx',
          'box-sizing': 'border-box',
          ...data.css
        },
        views: [
          {
            type: 'text',
            text: data.key ? item[data.key] : index,
            css: {
              'word-wrap': 'anywhere',
              padding: '0 2rpx',
              fontWeight: data.key ? 'bold' : '400'
            }
          }
        ]
      }
      detailViews.push(view)
    }
  }
  if (info.expenseDetails.length < 5) {
    for (var i = 0; i < 5 - info.expenseDetails.length; i++) {
      index++
      for (var data of keys) {
        var view = {
          type: 'view',
          css: {
            width: data.width,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            height: '34rpx',
            border: '0 solid #333333',
            borderBottomWidth: '1rpx',
            'box-sizing': 'border-box',
            ...data.css
          },
          views: [
            {
              type: 'text',
              text: data.key ? '' : index,
              css: {
                'word-wrap': 'anywhere',
                padding: '0 2rpx',
                fontWeight: data.key ? 'bold' : '400'
              }
            }
          ]
        }
        detailViews.push(view)
      }
    }
  }
  var selectedImage = baseUrl + 'xuanzhongzhifu@3x.png'
  var unselectedImage = baseUrl + 'weixuanzhongzhifu@3x.png'
  var payArray = []
  for (var item of ['微信', '支付宝', '转账', '现金', '其他']) {
    payArray.push({
      type: 'view',
      css: {
        width: '84rpx',
        height: '34rpx',
        display: 'flex',
        alignItems: 'center',
      },
      views: [
        {
          type: 'image',
          src: item == info.extend.payType ? selectedImage : unselectedImage,
          css: {
            width: '14rpx',
            height: '14rpx',
            marginRight: '8rpx'
          },
        },
        {
          type: 'text',
          text: item
        }
      ]
    })
  }
  return {
    css: {
      backgroundColor: '#FFFFFF',
      width: '702rpx',
      padding: '15rpx',
      boxSizing: "border-box",
      fontSize: '14rpx',
      'line-height': '17rpx'
    },
    views: [
     // 标题
     {
        type: 'view',
        css: {
          display: 'flex',
          justifyContent: 'center',
          alignItems: 'center',
          flexDirection: 'column',
          fontWeight: 'bold',
          textAlign: 'center',
          marginBottom: '28rpx',
          position: 'relative',
          width: '654rpx'
        },
        views: [
          {
            type: 'view',
            css: {
              height: '34rpx',
              display: 'flex',
              justifyContent: 'center',
              alignItems: 'center',
              position: 'relative',
              fontSize: '22rpx',
              marginTop: '10rpx'
            },
            views: [
              {
                type: 'text',
                text: info.repairShopName,
              }]
          },
          {
            type: 'view',
            css: {
              height: '6rpx',
              color: 'transparent',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              borderTopWidth: '1rpx',
              'box-sizing': 'border-box',
              position: 'relative',
              fontSize: '22rpx',
            },
            views: [
              {
                type: 'text',
                text: info.repairShopName,
              }]
          },
          {
            type: 'view',
            css: {
              top: 0,
              left: 0,
              position: 'absolute',
            },
            views: [
              {
                type: 'image',
                src: info.extend.logo  ? (info.extend.logo += '?imageView2/2/h/84') : '',
                css: {
                  'flex-shrink': 0,
                  height: '56rpx',
                  // width: '56rpx',
                  marginLeft: '28rpx'
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'flex-end',
              top: '24rpx',
              right: 0,
              position: 'absolute',
              color: '#875A38'
            },
            views: [
              {
                type: 'text',
                text: info.number,
                css: {
                  fontWeight: 'bold'
                }
              }
            ]
          }
        ]
      },
      // top 行
      {
        type: 'view',
        css: {
          width: '672rpx',
          display: 'flex',
          alignItems: 'flex-start',
          justifyContent: 'space-between',
          'min-height': '26rpx',
          marginBottom: '6rpx'
        },
        views: [
          {
            type: 'view',
            css: {
              display: 'flex',
              alignItems: 'flex-start',
            },
            views: [
              {
                type: 'text',
                text: '客户：'
              },
              {
                type: 'text',
                text: info.customer.name,
                css: {
                  fontWeight: 'bold'
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              display: 'flex',
              alignItems: 'flex-start',
            },
            views: [
              {
                type: 'text',
                text: '电话：'
              },
              {
                type: 'text',
                text: info.customer.phone,
                css: {
                  fontWeight: 'bold',
                  mingWidth: '100rpx'
                }
              }
            ]
          }
        ]
      },
      {
        type: 'view',
        css: {
          width: '672rpx',
          display: 'flex',
          alignItems: 'flex-start',
          justifyContent: 'space-between',
          'min-height': '26rpx',
          marginBottom: '6rpx'
        },
        views: [
          {
            type: 'view',
            css: {
              width: '470rpx',
              display: 'flex',
              alignItems: 'flex-start',
            },
            views: [
              {
                type: 'text',
                text: '客户地址：',
                css: {
                  "flex-shrink": 0
                }
              },
              {
                type: 'text',
                text: info.address,
                css: {
                  width: '410rpx',
                  fontWeight: 'bold'
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              width: '200rpx',
              display: 'flex',
              alignItems: 'flex-start',
              justifyContent: 'flex-end',
            },
            views: [
              {
                type: 'text',
                text: '交货日期：'
              },
              {
                type: 'text',
                text: info.repairTime.split(' ')[0],
                css: {
                  fontWeight: 'bold'
                }
              }
            ]
          }
        ]
      },
      // table
      {
        type: 'view',
        css: {
          border: '2rpx solid #333333',
          marign: '4rpx 0 8rpx 0',
          display: 'flex',
          alignItems: 'center',
          'flex-wrap': 'wrap',
          'flex-shrink': 0,
          justifyContent: 'space-between',
          'box-sizing': 'border-box',
        },
        views: [
          // 第一行
          {
            type: 'view',
            css: {
              width: '44rpx',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              'box-sizing': 'border-box',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              borderRightWidth: '1rpx',
            },
            views: [
              {
                type: 'text',
                text: '序号'
              }
            ]
          },
          {
            type: 'view',
            css: {
              width: '150rpx',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              'box-sizing': 'border-box',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              borderRightWidth: '1rpx',
            },
            views: [
              {
                type: 'text',
                text: '货名'
              }
            ]
          },
          {
            type: 'view',
            css: {
              width: '157rpx',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              'box-sizing': 'border-box',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              borderRightWidth: '1rpx',
            },
            views: [
              {
                type: 'text',
                text: '型号规格'
              }
            ]
          },
          {
            type: 'view',
            css: {
              width: '80rpx',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              'box-sizing': 'border-box',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              borderRightWidth: '1rpx',
            },
            views: [
              {
                type: 'text',
                text: '颜色'
              }
            ]
          },
          {
            type: 'view',
            css: {
              width: '80rpx',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              'box-sizing': 'border-box',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              borderRightWidth: '1rpx',
            },
            views: [
              {
                type: 'text',
                text: '单价'
              }
            ]
          },
          {
            type: 'view',
            css: {
              width: '80rpx',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              'box-sizing': 'border-box',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              borderRightWidth: '1rpx',
            },
            views: [
              {
                type: 'text',
                text: '数量'
              }
            ]
          },
          {
            type: 'view',
            css: {
              width: '80rpx',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              'box-sizing': 'border-box',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
            },
            views: [
              {
                type: 'text',
                text: '金额'
              },
            ]
          },
          // 数据行
          ...detailViews,
          // 合计行
          {
            type: 'view',
            css: {
              width: '672rpx',
              display: 'flex',
              alignItems: 'center',
              'box-sizing': 'border-box',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
            },
            views: [
              {
                type: 'view',
                css: {
                  display: 'flex',
                  alignItems: 'center',
                  padding: '0 12rpx 0 14rpx',
                  'box-sizing': 'border-box',
                  'min-width': '156rpx',
                  height: '34rpx',
                },
                views: [
                  {
                    type: 'text',
                    text: '合计金额（大写）',
                    css: {
                      textAlign: 'center'
                    }
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  display: 'flex',
                  alignItems: 'center',
                  width: '328rpx',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                },
                views: [
                  ...moneyArray
                ]
              },
              (info.extend.sealType == '1' || info.extend.sealType == '2') && info.extend.sealUrl ?
                {
                  type: 'view',
                  css: {
                    width: '1rpx',
                    height: '34rpx',
                    display: 'flex',
                    alignItems: 'center',
                  },
                  views: [
                    {
                      type: 'image',
                      src: info.extend.sealUrl,
                      css: {
                        'flex-shrink': 0,
                        height: '96rpx',
                        marginLeft: info.extend.sealType == '2' ? '48rpx' : 0
                      }
                    }
                  ]
                } : {},
              {
                type: 'view',
                css: {
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'flex-end',
                  width: '80rpx',
                  height: '52rpx',
                },
                views: [
                  {
                    type: 'text',
                    text: '￥' + formatCount(info.receivableAmount),
                    css: {
                      'white-space': 'nowrap',
                      fontWeight: 'bold'
                    }
                  }
                ]
              }
            ]

          },
          // 付款方式行
          {
            type: 'view',
            css: {
              width: '672rpx',
              display: 'flex',
              alignItems: 'center',
              height: '34rpx',
              border: '0 solid #333333',
              borderBottomWidth: '1rpx',
              'box-sizing': 'border-box',
              paddingLeft: '14rpx'
            },
            views: [
              {
                type: 'view',
                css: {
                  'min-width': '74rpx',
                  display: 'flex',
                  alignItems: 'center',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                  marginRight: '10rpx'
                },
                views: [
                  {
                    type: 'text',
                    text: '付款方式：',
                    css: {
                      textAlign: 'center'
                    }
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  display: 'flex',
                  alignItems: 'center',
                  width: '440rpx',
                  'box-sizing': 'border-box',
                  height: '34rpx',
                },
                views: [
                  ...payArray
                ]
              },
            ]
          },
          {
            type: 'view',
            css: {
              width: '672rpx',
              display: 'flex',
              alignItems: 'flex-start',
              'box-sizing': 'border-box',
              padding: '8rpx 14rpx',
              minHeight: '96rpx'
            },
            views: [
              {
                type: 'view',
                css: {
                  display: 'flex',
                  alignItems: 'center',
                  marginRight: '10rpx'
                },
                views: [
                  {
                    type: 'text',
                    text: '备注说明：',
                    css: {
                      textAlign: 'center',
                      "flex-shrink": 0
                    }
                  }
                ]
              },
              {
                type: 'view',
                css: {
                  display: 'flex',
                  alignItems: 'center',
                },
                views: [
                  {
                    type: 'text',
                    text: info.remark || ' ',
                    css: {
                      fontWeight: 'bold',
                      width: '562rpx',
                      'line-height': '18rpx'
                    }
                  }
                ]
              },
            ]

          },
        ]
      },
      // bottom 行
      {
        type: 'view',
        css: {
          width: '100%',
          display: 'flex',
          alignItems: 'center',
          height: '34rpx',
          marginTop: '4rpx'
        },
        views: [
          {
            type: 'view',
            css: {
              width: '224rpx',
              display: 'flex',
              alignItems: 'center',
            },
            views: [
              {
                type: 'text',
                text: '应收金额：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: '￥' + formatCount(info.receivableAmount),
                css: {
                  fontWeight: 'bold'
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              width: '224rpx',
              display: 'flex',
              alignItems: 'center',
            },
            views: [
              {
                type: 'text',
                text: '实收金额：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: '￥' + formatCount(info.actualAmount),
                css: {
                  fontWeight: 'bold'
                }
              }
            ]
          }, {
            type: 'view',
            css: {
              width: '224rpx',
              display: 'flex',
              alignItems: 'center',
            },
            views: [
              {
                type: 'text',
                text: '欠款金额：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: '￥' + formatCount(Math.max(Number(info.receivableAmount) - Number(info.actualAmount), 0)),
                css: {
                  fontWeight: 'bold'
                }
              }
            ]
          }
        ]
      },
      {
        type: 'view',
        css: {
          width: '100%',
          display: 'flex',
          alignItems: 'flex-start',
          height: '34rpx',
          marginTop: '4rpx'
        },
        views: [
          {
            type: 'view',
            css: {
              width: '224rpx',
              display: 'flex',
              alignItems: 'flex-start',
            },
            views: [
              {
                type: 'text',
                text: '收款人：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'text',
                text: info.lister,
                css: {
                  fontWeight: 'bold',
                }
              }
            ]
          }, info.extend.customerSignSwitch ? {
            type: 'view',
            css: {
              width: '224rpx',
              display: 'flex',
              alignItems: 'flex-start',
            },
            views: [
              {
                type: 'text',
                text: '客户签名：',
                css: {
                  'flex-shrink': 0,
                }
              },
              {
                type: 'image',
                src: info.extend.customerSign += '?imageView2/2/h/40',
                css: {
                  height: '24rpx',
                }
              }
            ]
          } : {
          }
        ]
      },
    ]
  }
}
export default doorsViews